Microsoft DirectX 8.1 (C++)

Step 2. Select a Capture Device

The second step is to select a capture device, by using the System Device Enumerator. For more information, see Using the System Device Enumerator. The following code example enumerates the video capture devices and chooses the first device in the enumeration sequence:

// Create the system device enumerator.
ICreateDevEnum *pDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, 
    IID_ICreateDevEnum, (void **)&pDevEnum);

// Create an enumerator for video capture devices.
IEnumMoniker *pClassEnum = NULL;
pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0);

ULONG cFetched;
IMoniker *pMoniker = NULL;
IBaseFilter *psrc = null;
if (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
    // Bind the first moniker to a filter object.
    pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
    pMoniker->Release();
}
pClassEnum->Release();
pDevEnum->Release();

It would be more typical to have the user choose a device—for example, by displaying a list box with the names of available devices. To do this, call the IMoniker::BindToStorage method to obtain a property bag for the device moniker, and call the IPropertyBag::Read method to retrieve the device's friendly name. For more information, see Using the System Device Enumerator.

Now add the capture filter to the graph:

// pSrc is the capture filter from the previous code example.
pGraph->AddFilter(pSrc, L"Video Capture");

Note   A device that captures both audio and video appears in two categories: Audio Capture Sources and Video Capture Sources. After you select a device from one category, check the media types on the filter's output pins. If the device falls into both categories, don't add a second device. This also applies to digital video (DV) cameras that produce interleaved audio/video.